package com.guoyansoft.controller;

import com.guoyansoft.config.R;
import com.guoyansoft.model.pojo.Goods;
import com.guoyansoft.model.pojo.GoodsType;
import com.guoyansoft.model.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {
@Autowired
private GoodsService goodsService;
    @RequestMapping("/selectByName")
    public R selectByName(Integer curPage, Integer pageSize,
                          String shopName) {
        Map<String,Object> params=new HashMap<>();
        params.put("shopTypeNameKey","%"+shopName+"%");
        params.put("startIndexKey",(curPage-1)*pageSize);
        params.put("pageSizeKey",pageSize);
        R r = goodsService.selectByPageAndTotalsize(params);
        return r;
    }
    @RequestMapping("/selectByShopId")
    public R selectByShopId(Integer curPage, Integer pageSize,String shopId,String name,Integer goodsTypeId) {
        Map<String,Object> params=new HashMap<>();
        params.put("startIndexKey",(curPage-1)*pageSize);
        params.put("pageSizeKey",pageSize);
        String[] split = shopId.split(",");
        params.put("shopIdKey",split[0]);
        params.put("nameKey","%"+name+"%");
        params.put("goodsTypeIdKey",goodsTypeId);
        return goodsService.selectByPageAndshopId(params);
    }
    @RequestMapping("/selectByShopTypeId")
    public R selectByShopTypeId(Integer curPage, Integer pageSize,Integer shopTypeId) {
        Map<String,Object> params=new HashMap<>();
        params.put("startIndexKey",(curPage-1)*pageSize);
        params.put("pageSizeKey",pageSize);
        params.put("shopTypeId",shopTypeId);
        R r = goodsService.selectByShopTypeId(params);
        return r;
    }

    //查询所有的物品
    @RequestMapping("/queryByPage")
    public R queryByPage(Integer curPage,Integer pageSize,
                         Double maxSalary) {
        Map<String,Object> params=new HashMap<>();
        params.put("startIndexKey",(curPage-1)*pageSize);
        params.put("pageSizeKey",pageSize);
        R r = goodsService.queryByPage(params);
        return r;
    }
    //    批量删除
    @RequestMapping("/dropBatch")
    public R dropBatch(Integer[] id,Integer shopId) {
        return goodsService.dropBatch(id,shopId);
    }
    @RequestMapping("/save")
    public R save(Goods goods, Integer goodsTypeId,Integer shopId) {
        GoodsType goodsType=new GoodsType();
        goodsType.setId(goodsTypeId);
        goods.setGoodsType(goodsType);
        return goodsService.save(goods,shopId);
    }
    @RequestMapping("/modify")
    public R modify(Goods goods,Integer goodsTypeId){
        GoodsType goodsType=new GoodsType();
        goodsType.setId(goodsTypeId);
        goods.setGoodsType(goodsType);
        return goodsService.update(goods);
    }

    @RequestMapping("/queryById")
    public R queryById(Integer id){
        Goods goods = goodsService.queryById(id);
        return goods==null?R.suc():R.fail();
    }

    @RequestMapping("/queryPageAndShopId")
    public R queryPageAndShopId(Integer curPage, Integer pageSize,Integer shopId){
        Map<String,Object> params=new HashMap<>();
        params.put("startIndexKey",(curPage-1)*pageSize);
        params.put("pageSizeKey",pageSize);
        params.put("shopIdKey",shopId);
        return goodsService.queryPageAndShopId(params);
    }
}
